package com.atguigu.yygh.user.controller.api;

import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.utils.AuthContextHolder;
import com.atguigu.yygh.model.user.Patient;
import com.atguigu.yygh.user.service.PatientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author ybl
 * @version 1.9
 * @date 2021/5/22 10:10
 * 就诊人管理
 */
@Api(tags = "就诊人增删改查")
@RestController
@RequestMapping("/api/user/patient")
public class PatientApiController {

  @Autowired
  private PatientService patientService;


  // 根据用户id获取所有就诊人
  @ApiOperation(value = "获取所有就诊人")
  @GetMapping("auth/findAll")
  public Result findAll(HttpServletRequest request){
    Long userId = AuthContextHolder.getUserId(request);
    List<Patient> list = patientService.findAll(userId);
    return Result.ok(list);
  }

  // 添加就诊人信息
  @ApiOperation(value = "添加就诊人信息")
  @PostMapping("auth/save")
  public Result savePatient(@RequestBody Patient patient,HttpServletRequest request) {
    Long userId = AuthContextHolder.getUserId(request);
    patient.setUserId(userId);
    patientService.save(patient);
    return Result.ok();
  }

  // 修改就诊人信息
  @ApiOperation(value = "修改就诊人信息")
  @PostMapping("auth/update")
  public Result updatePatient(@RequestBody Patient patient) {
    patientService.updateById(patient);
    return Result.ok();
  }

  // 删除就诊人信息
  @DeleteMapping("auth/remove/{id}")
  public Result delPatient(@PathVariable Long id) {
    patientService.removeById(id);
    return Result.ok();
  }

  // 根据id获取就诊人信息
  @GetMapping("auth/get/{id}")
  public Result getById(@PathVariable Long id) {
    Patient patient = patientService.getPatientId(id);
    return Result.ok(patient);
  }

  @ApiOperation(value = "获取就诊人信息")
  @GetMapping("inner/get/{id}")
  public Patient getPatientOrder(@PathVariable Long id) {
    return patientService.getById(id);
  }
}
